package com.wzw.springboot.principle.mvc.interceptors;

import org.springframework.web.servlet.AsyncHandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * HandlerInterceptorAdapter: 已过期的类，不建议使用了。
 *                            这个类本身什么每做，只是把接口中的方法都默认实现了，用于简化。可能具体实现类并不想实现全部的方法。此适配模式值得学习
 * AsyncHandlerInterceptor： HandlerInterceptorAdapter 的父接口，文档建议直接实现这个
 *                           这个类也是 HandlerInterceptorAdapter 的子节点，只不过增强了一个异步请求时拦截回调的方法
 * @author Wangzhiwen
 */
public class CustomizedInterceptorTwo implements AsyncHandlerInterceptor {

    /**
     * AsyncHandlerInterceptor中补充的方法，这个方法会在Controller方法异步执行时开始执行
     * @param request httpRequest
     * @param response httpResponse
     * @param handler 执行器
     * @throws Exception 异常
     */
    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        AsyncHandlerInterceptor.super.afterConcurrentHandlingStarted(request, response, handler);
    }

    /**
     * 前置拦截
     *
     * @param request  httRequest
     * @param response httpResponse
     * @param handler  处理器
     * @return true 放行 false 不放行
     * @throws Exception 异常
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return AsyncHandlerInterceptor.super.preHandle(request, response, handler);
    }

    /**
     * 执行controller方法后回调，需要等到Controller的异步执行完才能执行
     *
     * @param request      httRequest
     * @param response     httpResponse
     * @param handler      处理器
     * @param modelAndView 执行方法后的返回的视图对象
     * @throws Exception 异常
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        AsyncHandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    /**
     * 方法执行完成后的拦截，不管执行结果是成功还是失败
     *
     * @param request  httRequest
     * @param response httpResponse
     * @param handler  处理器
     * @param ex       执行过程异常时被抛出的异常对象
     * @throws Exception 异常
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        AsyncHandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
